package com.msh.artascope.devcloud.service.api;

import com.msh.artascope.devcloud.client.po.EnvPo;
import com.msh.artascope.devcloud.service.model.page.ProjectListPageQo;
import com.msh.artascope.devcloud.service.model.page.ProjectListPageVo;
import com.msh.artascope.devcloud.service.model.query.ProductQueryVo;
import com.msh.artascope.devcloud.service.model.query.ProjectQueryVo;
import com.msh.artascope.devcloud.service.model.vo.ProjectVo;
import com.msh.frame.client.common.Result;
import com.msh.frame.client.exception.BizException;
import com.msh.frame.client.model.TreeVo;
import com.msh.frame.client.validation.*;
import com.msh.artascope.interfaces.IAccountGet;
import com.msh.artascope.interfaces.ILoginAccount;
import com.msh.frame.web.base.BaseController;
import com.msh.artascope.devcloud.client.po.ProjectPo;
import com.msh.artascope.devcloud.client.qo.ProjectQo;
import com.msh.artascope.devcloud.service.service.ProjectService;
import javax.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.groups.Default;
import java.util.List;


/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Tag(name = "项目信息-api相关接口")
@Slf4j
@RestController
@RequestMapping("/api/project")
public class ProjectApi extends BaseController<ProjectPo, ProjectQo> {

  @Autowired
  private ProjectService projectService;

  @Autowired
	private IAccountGet accountGet;


  @Operation(summary = "通用插入逻辑")
  @PostMapping("insert")
  public Result<ProjectVo> insert(@Validated({IInsertVo.class,Default.class}) @RequestBody ProjectVo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    p.initLoginAccount(loginAccount);
    return Result.successReturn(projectService.insertVo(p));
  }

  @Operation(summary = "通用更新逻辑")
  @PostMapping("update")
  public Result update(@Validated({IUpdateVo.class,Default.class}) @RequestBody ProjectVo p) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    p.initLoginAccount(loginAccount);
    projectService.update(p);
    return Result.successReturn();
  }


  @Operation(summary = "通用删除逻辑")
  @GetMapping("delete")
  public Result delete(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    projectService.delete(id, loginAccount.getTenantId());
    return Result.successReturn();
  }

  @Operation(summary = "获取详情")
  @GetMapping("get")
  public Result<ProjectVo> get(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(projectService.getVo(id, loginAccount.getTenantId()));
  }

  @Operation(summary = "vo列表查询")
  @PostMapping("list")
  public Result<List<ProjectVo>> list(@Validated({IListVo.class}) @RequestBody ProjectQueryVo q) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return Result.successReturn(projectService.listVo(q));
  }


  @Operation(summary = "vo列表分页查询")
  @PostMapping("page")
  public Result<List<ProjectVo>> page(@Validated({IListVo.class}) @RequestBody ProjectQueryVo q) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return projectService.pageVo(q);
  }

  @Operation(summary = "获取详情")
  @GetMapping("getByModuleId")
  public Result<ProjectPo> getByModuleId(@NotNull(message = "moduleId不能为空") @RequestParam("moduleId") Long moduleId) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(projectService.getByModuleId(moduleId, loginAccount.getTenantId()));
  }

  @Operation(summary = "通过产品id获取项目列表页Vo")
  @GetMapping("projectListPageVoByModuleId")
  public Result<ProjectListPageVo> projectListPageVoByModuleId(@NotNull(message = "moduleId不能为空") @RequestParam("moduleId") Long moduleId){
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(projectService.projectListPageVoByModuleId(moduleId, loginAccount.getTenantId()));
  }

  @Operation(summary = "通过产品id获取项目列表页Vo")
  @PostMapping("projectListPageVo")
  public Result<ProjectListPageVo> projectListPageVo(@Validated @RequestBody ProjectListPageQo projectListPageQo){
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(projectService.projectListPageVo(projectListPageQo, loginAccount.getTenantId()));
  }

  @Operation(summary = "获取可复制环境列表")
  @GetMapping("copyEnableEnv")
  public Result<List<EnvPo>> copyEnableEnv(@NotNull(message = "projectName不能为空") @RequestParam("projectName") String projectName){
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(projectService.copyEnableEnv(projectName, loginAccount.getTenantId()));
  }

  @Operation(summary = "复制环境")
  @GetMapping("copy")
  public Result<ProjectPo> copy(@NotNull(message = "id不能为空") @RequestParam("id") Long id,
      @NotNull(message = "envId不能为空") @RequestParam("envId") Long envId){
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(projectService.copy(id, envId, loginAccount.getTenantId()));
  }

  @Operation(summary = "项目产品树结构查询")
  @PostMapping("listProductProjectTree")
  public Result<List<TreeVo>> listProductProjectTree(@Validated({IListVo.class}) @RequestBody ProjectQueryVo q){
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return Result.successReturn(projectService.listProductProjectTree(q));
  }
}
